#include <stdio.h>


/* PAGE_SHIFT determines the page size */
#define PAGE_SHIFT		12
#define PAGE_SIZE		(1UL << PAGE_SHIFT)
#define PAGE_MASK		(~(PAGE_SIZE-1))

#define MMZ_GRAIN PAGE_SIZE

#define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))

#define mmz_align2(x,g) ((((x)+(g)-1)/(g))*(g))
#define mmz_grain_align(x) mmz_align2(x,MMZ_GRAIN)
#define mmz_length2grain(len) (mmz_grain_align(len)/MMZ_GRAIN)

#define mmz_bitmap_size(p) (mmz_align2(mmz_length2grain((p)->nbytes),8)/8)


void _align2(int x, int a)
{
	int temp;
	int ret;

	printf("----------- function : %s -----------\n", __FUNCTION__);
	temp = x + a - 1;
	printf("temp = %d \n", temp);
	ret = ~(a-1);
	printf("ret = %d \n", ret);

	printf("______align2 = %d \n", temp & ret);

}

int _mmz_align2(int x, int g)
{
	int temp;
	int ret;

	printf("----------- function : %s -----------\n", __FUNCTION__);
	temp = (x + g) -1;
	printf("temp = %d \n", temp);
	temp /= g;
	printf("temp = %d \n", temp);
	ret = temp * g;
	printf("______mmz_align2 = %d \n", ret);

	return ret;
}

int _mmz_grain_align(int x)
{
	int ret;

	printf("----------- function : %s -----------\n", __FUNCTION__);
	ret =  _mmz_align2(x, MMZ_GRAIN);
	printf("______mmz_grain_align = %d\n",ret);

	return ret;
}

void _mmz_length2grain(int len)
{
	printf("----------- function : %s -----------\n", __FUNCTION__);
	printf("______mmz_length2grain = %d\n", _mmz_grain_align(len) / MMZ_GRAIN);
}

void nor()
{
	unsigned long fixed_len=~1;

	printf("0x%02x\n", fixed_len);
	printf("0x%02x\n", !fixed_len);

	fixed_len = !!!!fixed_len;
	printf("0x%02x\n", fixed_len);
}

int main()
{
	printf("align = %d\n", ALIGN(4, 2));
	printf("mmz_align2 = %d\n", mmz_align2(4, 2));
	
	printf("\nMMZ_GRAIN = %d\n", MMZ_GRAIN);
	printf("mmz_grain_align = %d\n", mmz_grain_align(8));
	printf("mmz_length2grain = %d\n", mmz_length2grain(4));
//	printf("mmz_bitmap_size = %d\n", mmz_bitmap_size(4));

//	putchar(0xa);
//	_align2(4,2);
//	putchar(0xa);

	_mmz_align2(4,8);
//	putchar(0xa);

//	_mmz_grain_align(8);
//	putchar(0xa);

	_mmz_length2grain(4);

	putchar(0xa);
	nor();

	void view();
	view();

	return 0;
}



#define PMD_SHIFT		21
#define PGDIR_SHIFT		21

#define PMD_SIZE		(1UL << PMD_SHIFT)	// 0x200000		= 2097152
#define PMD_MASK		(~(PMD_SIZE-1))		// 0xffe00000	= -2097152
#define PGDIR_SIZE		(1UL << PGDIR_SHIFT)// 0x200000		= 2097152
#define PGDIR_MASK		(~(PGDIR_SIZE-1))	// 0xffe00000	= -2097152

void view()
{
	printf("PMD_SIZE 0x%x\n", PMD_SIZE);
	printf("PMD_MASK 0x%x\n", PMD_MASK);
	printf("PGDIR_SIZE 0x%x\n", PGDIR_SIZE);
	printf("PGDIR_MASK 0x%x\n", PGDIR_MASK);
}


// http://news.cntv.cn/program/tianwang/20110104/113242.shtml
